const app = getApp();
const Controller = require('../controllers/controllers.js').controller;
const baseUrl = require('../controllers/controllers.js').baseUrl;
Page({
  data: {
    id:'',
    detail:'',
    desc:'',
    resondata:[{name:"多拍"},{name:"拍错"},{name:"不想要"},{name:"等太久了"},{name:"不想再等了～"}],
    reson:'',
    graphimg:[],
    resonindex:"",
  },
  onLoad(options) {
    if(options.id){
      this.setData({
        id:options.id
      })
      this.orderdetail()
    }
  },
  bindPickerChange: function(e) {
    this.setData({
      reson:this.data.resondata[e.detail.value].name,
      resonindex:e.detail.value
    })
  },
  orderdetail(){
    Controller.orderdetail({orderNumber:this.data.id}).then(res=>{
      if(res.data.baseData){
        this.setData({
          detail:res.data.baseData,
        })
      }
    })
  },
  changedesc(e){
    this.setData({
      desc:e.detail.value
    })
  },
  changereson(e){
    this.setData({
      reson:e.detail.value
    })
  },
  selectgraph: function (type) {
    var that = this;
    wx.chooseImage({
      count:3,
      sizeType: ['original', 'compressed'],
      sourceType: ["album"],
        success: function (res) {
          var img = res.tempFilePaths[0];
          that.picture(img)
        },
        fail: function (res) {
          console.log(res)
        }
    })
  },
  picture(e){
    var that=this;
    wx.uploadFile({
        url: baseUrl+'/upload/picture',
        filePath: e,
        name: "file",
        formData: {
        },
        success: function (res) {
          that.setData({
            graphimg:that.data.graphimg.concat(JSON.parse(res.data).data.baseData[0]),
          })
        }
    })
  },
  delete(e){
    var graphimg=this.data.graphimg;
    var index=e.currentTarget.dataset.index
    graphimg.splice(index,1)
    this.setData({
      graphimg
    })
  },
  next(){
    if(!this.data.reson){
      wx.showToast({
        title:"请输入退款原因",
        icon:"none"
      })
      return
    }
    // if(this.data.graphimg.length<=0){
    //   wx.showToast({
    //     title:"请上传凭证",
    //     icon:"none"
    //   })
    //   return
    // }
    var data={
      orderNumber:this.data.id,
      remark:this.data.desc,
      action:this.data.reson,
      imgUrls:this.data.graphimg.join(','),
    }
    Controller.refundapply(data).then(res=>{
     
        wx.showToast({
          title:"提交成功",
          icon:"none"
        })
        if(this.data.detail.refundStatus==1){
          setTimeout(()=>{
            wx.redirectTo({
              url:'/pages/service/address?id='+this.data.id
            })
          },1000)
        }else{
          setTimeout(()=>{
            wx.redirectTo({
              url:'/pages/service/result?id='+this.data.id
            })
          },1000)
        }

    })
    
  },
  onShareAppMessage: function (e) {
   
  },
  onShareTimeline: function(res){
    
  },
})